package xmu.zfw.vo;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;

import org.apache.commons.lang.StringEscapeUtils;
import lombok.Data;
import xmu.zfw.domain.Report;
import xmu.zfw.domain.Reporteder;
import xmu.zfw.domain.Reporter;
import xmu.zfw.util.CheckNull;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
public class ReportVO {
    private Integer id;
    private String no;
    @NotNull
    private String name;
    @ApiModelProperty(example = "11111")
    private String cardId;
    private String location;

    @ApiModelProperty(example = "[{\"blackName\": \"liublack\", \"blackDepartment\": \"xmu\"}]")
    @NotEmpty
    private JSONArray blackInfo;
    private String question;

    @ApiModelProperty(example = "是")
    private String isSend;

    @ApiModelProperty(example = "是")
    private String moreLetter;

    private String involvedUnit;
    private String involvedRegion;
    private String involvedArea;
    private String enterer;

    @ApiModelProperty(example = "信访")
    private String reportType;
    private String mailNo;

    //    @ApiModelProperty(example = "2018-10-01 12:18:48")
//    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",  timezone="GMT+8")
    @ApiModelProperty(example = "2018-10-01")
    @JsonFormat(pattern = "yyyy-MM-dd",  timezone="GMT+8")
    private String sendTime;

    private String sendDepartment;
    private String feedbackTimeLimit;

    @ApiModelProperty(example = "2018-10-01")
    @JsonFormat(pattern = "yyyy-MM-dd",  timezone="GMT+8")
    private String feedbackTime;

    private String conclusion;
    private String progress;

    @ApiModelProperty(example = "督办")
    private String level;

    @ApiModelProperty(example = "是")
    private String isRepeat;

    @ApiModelProperty(example = "111111")
    private String phone;


    public static ReportVO valueOf(Report report){
        ReportVO reportVO = new ReportVO();
        reportVO.setId(report.getId());
        reportVO.setReporter(report.getReporter());
        reportVO.setReporteder(report.getReporteders());
        reportVO.setQuestion(report.getQuestion());
        reportVO.setEnterer(report.getEnterer());
        reportVO.setQuestion(report.getQuestion());
        reportVO.setIsSend(report.getIsSend());
        reportVO.setMoreLetter(report.getMoreLetter());
        reportVO.setInvolvedUnit(report.getInvolvedUnit());
        reportVO.setInvolvedArea(report.getInvolvedArea());
        reportVO.setInvolvedRegion(report.getInvolvedRegion());
        reportVO.setReportType(report.getReportType());
        reportVO.setMailNo(report.getMailNo());
        if(report.getSendTime() != null){
            reportVO.setSendTime(report.getSendTime().toString());
        }
        reportVO.setSendDepartment(report.getSendDepartment());
        if(report.getFeedbackTime() != null){
            reportVO.setFeedbackTime(report.getFeedbackTime().toString());
        }
        reportVO.setFeedbackTimeLimit(report.getFeedbackTimeLimit());
        reportVO.setConclusion(report.getConclusion());
        reportVO.setProgress(report.getProgress());
        reportVO.setLevel(report.getLevel());
        reportVO.setIsRepeat(report.getIsRepeat());
        reportVO.setNo(report.getNo());
        try {
            CheckNull.chekNull(reportVO);
        }catch (Exception e){
            e.printStackTrace();
        }
        return reportVO;
    }

    public Report toReport(){
        Report report = new Report();
        report.setId(getId());
        report.setReporter(getReporter());
        report.setReporteders(getReporteders());
        report.setQuestion(getQuestion());
        report.setEnterer(getEnterer());
        report.setQuestion(getQuestion());
        report.setIsSend(getIsSend());
        report.setMoreLetter(getMoreLetter());
        report.setInvolvedUnit(getInvolvedUnit());
        report.setInvolvedArea(getInvolvedArea());
        report.setInvolvedRegion(getInvolvedRegion());
        report.setReportType(getReportType());
        report.setMailNo(getMailNo());
        if(getSendTime() != null && !getSendTime().equals("")){
            report.setSendTime(Date.valueOf(getSendTime()));
        }
        report.setSendDepartment(getSendDepartment());
        if(getFeedbackTime() != null && !getFeedbackTime().equals("")) {
            report.setFeedbackTime(Date.valueOf(getFeedbackTime()));
        }
        report.setFeedbackTimeLimit(getFeedbackTimeLimit());
        report.setConclusion(getConclusion());
        report.setProgress(getProgress());
        report.setLevel(getLevel());
        report.setIsRepeat(getIsRepeat());
        report.setNo(getNo());
        return report;
    }

    public static List<ReportVO> getReportVOs(List<Report> reportList){
        List<ReportVO> reportVOList = new ArrayList<>();
        for(Report report: reportList){
            reportVOList.add(ReportVO.valueOf(report));
        }
        return reportVOList;
    }


    private void setReporter(Reporter reporter){
        setName(reporter.getName());
        setLocation(reporter.getLocation());
        setPhone(reporter.getPhone());
        setCardId(reporter.getCardId());
    }

    private void setReporteder(List<Reporteder> reporteders){
        JSONArray jsonArray = new JSONArray();
        for(Reporteder reporteder: reporteders){
            JSONObject reportederJson = new JSONObject();
            reportederJson.put("blackName", reporteder.getName());
            reportederJson.put("blackDepartment", reporteder.getDepartment());
            jsonArray.add(reportederJson);
        }
        setBlackInfo(jsonArray);
    }

    private Reporter getReporter(){
        if(this.getLocation() == null
            && this.getName() == null
            && this.getPhone() == null
            && this.getCardId() == null){
            return null;
        }
        Reporter reporter = new Reporter();
        reporter.setLocation(this.getLocation());
        reporter.setName(this.getName());
        reporter.setPhone(this.getPhone());
        reporter.setCardId(this.getCardId());
        return reporter;
    }

    private List<Reporteder> getReporteders(){
        if(getBlackInfo() == null){
            return new ArrayList<Reporteder>();
        }
        List<Reporteder> reporteders = new ArrayList<>();

        for(int i = 0; i < getBlackInfo().size(); i++){
            Reporteder reporteder = new Reporteder();
            JSONObject jsonObject = getBlackInfo().getJSONObject(i);
            reporteder.setName(jsonObject.getString("blackName"));
            reporteder.setDepartment(jsonObject.getString("blackDepartment"));
            reporteders.add(reporteder);
        }
        return reporteders;
    }
}
